package com.a2.appa3.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.a2.appa3.R;
import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.global.Info;
import com.a2.appa3.model.CustomObserver;
import com.a2.appa3.model.network.HttpJsonUtils;
import com.a2.appa3.model.network.entity.req.changeNikeNameBean;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by admin on 2018/12/26.
 */

public class changeNikeDiolog extends Activity {
    private EditText changenike;
    private TextView cancle;
    private TextView conform;
    private String roomid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_nike_layout);
        changenike = findViewById(R.id.changenike);
        cancle = findViewById(R.id.btn_neg);
        conform = findViewById(R.id.btn_pos);
        roomid = getIntent().getStringExtra("grounpid");
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        conform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (changenike.getText().toString().isEmpty()) {
                    Toast.makeText(changeNikeDiolog.this, "输入昵称不能为空", Toast.LENGTH_SHORT).show();
                } else {

                    if (!TextUtils.isEmpty(roomid)) {
                        //修改群聊昵称
                        changegroupnikename();
                    } else {
                        //修改个人昵称
                        changeusernikename();
                    }
                }
            }
        });
    }

    private void changegroupnikename() {
        HttpJsonUtils.getHttpApiInstance()
                .changegroupnikename(HttpAdress.getuseAdress() + "api/chatapi/ChatRoom/setRoomNickname",
                        changenike.getText().toString().trim(), roomid, Info.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<changeNikeNameBean>() {
                    @Override
                    protected void onCustomNext(changeNikeNameBean resBanner) {
                        if (resBanner.getCode() == 0) {
                            Toast.makeText(changeNikeDiolog.this, "修改昵称成功", Toast.LENGTH_LONG).show();
                            finish();
                        } else {
                            Toast.makeText(changeNikeDiolog.this, "" + resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }

    private void changeusernikename() {
        HttpJsonUtils.getHttpApiInstance()
                .change_niocheng(HttpAdress.getuseAdress() + "api/mobile/changeName", changenike.getText().toString().trim(), Info.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<changeNikeNameBean>() {
                    @Override
                    protected void onCustomNext(changeNikeNameBean resBanner) {
                        if (resBanner.isStatus()) {
                            Info.user.nickname = changenike.getText().toString().trim();
                            Toast.makeText(changeNikeDiolog.this, "修改昵称成功", Toast.LENGTH_LONG).show();
                            finish();
                        } else {
                            Toast.makeText(changeNikeDiolog.this, "" + resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }
}
